//package com.cmy.web.init;
//
//import cn.hutool.db.Db;
//import cn.hutool.db.Entity;
//import cn.hutool.db.ds.simple.SimpleDataSource;
//import com.cmy.common.utils.MyFileUtil;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.io.FilenameUtils;
//import org.springframework.core.env.ConfigurableEnvironment;
//
//import java.io.*;
//import java.nio.charset.StandardCharsets;
//import java.sql.SQLException;
//import java.util.List;
//import java.util.stream.Collectors;
//
///**
// * @Author:Cmy
// * @Date:2023-03-07 13:23
// * 利用生命周期 在
// */
//@Slf4j
//public class DBInitializer{
//
//    public DBInitializer(ConfigurableEnvironment environment) {
//        this.environment = environment;
//    }
//
//    private ConfigurableEnvironment environment;
//
//
//    public void initialDB(){
//
//        String name = environment.getProperty("spring.datasource.name");
//        String url = environment.getProperty("spring.datasource.url");
//        String username = environment.getProperty("spring.datasource.username");
//        String password = environment.getProperty("spring.datasource.password");
//
//        //读取类路径下的文件
//        String mysqlUrl = url.replace(name, "mysql");
//        SimpleDataSource mysqlDb = new SimpleDataSource(mysqlUrl, username, password);
//
//        try {
//            List<Entity> schemaNameList = Db.use(mysqlDb).find(Entity.create("information_schema.SCHEMATA").set("SCHEMA_NAME", name));
//            if (schemaNameList.isEmpty()){//不存在该数据库
//                log.info("\n========================================\n" +
//                         "db:{} does not exist begin to initialize\n" +
//                         "========================================",name);
//
//                String createDbSql = "CREATE DATABASE IF NOT EXISTS `"+name+"` DEFAULT CHARACTER SET = `utf8` COLLATE `utf8_general_ci`;";
//                int execute = Db.use(mysqlDb).execute(createDbSql);
//                if (execute>0){
//                    log.info("\n...create db : {} success, next step => switch dataSource and execute initSql...",name);
//                    SimpleDataSource theDb = new SimpleDataSource(url, username, password);
//                    String initPath = FilenameUtils.separatorsToSystem("db\\initdb\\"+name+".sql");
//                    try (InputStream inputStream = MyFileUtil.readFileForJar(initPath)) {
//
//                        String sql = new BufferedReader(
//                                new InputStreamReader(inputStream, StandardCharsets.UTF_8))
//                                .lines()
//                                .collect(Collectors.joining("\n"));
//
//                        int initializer = Db.use(theDb).execute(sql);
//                        log.info("\n...initializer db success..:{}",initializer);
//                    };
//                }
//
//            }else {
//                log.info("\n========================================\n" +
//                        "db:{} has already existed \n" +
//                        "========================================",name);
//            }
//        } catch (SQLException throwAbles) {
//            throwAbles.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//
//    }
//
//
//}
//
//